//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "A2Point.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
 : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Open1Click(TObject *Sender)
{
  FILE *fp;
  LONG fsize;

  FOpen1->InitialDir = cur_dir; 
  if ( FOpen1->Execute( ) ){
    fp = fopen( FOpen1->FileName.c_str( ), "rb" );
    if ( !fp ){
      ShowMessage( "Error: Could not open file." );
      return;
    }

    //
    fsize = FileSize( fp );
    fread( rom, 1, fsize, fp );
    fclose( fp );

    // Build the pointer Table
    BuildTable( rom, fsize );
  }
}


//-----------------------------//
//                             //
//-----------------------------//
long __fastcall TForm1::BuildTable( BYTE *data, WORD *ptr, LONG Size )
{
  FILE *tf;
  LONG posi;
  WORD point,
       valu;

//  memset( ptr, 0, 0x200 * sizeof(WORD));//
  for ( int c = 0; c < 0x100; c++ )
    ptr[ c ] = 0xA002;

  // Start process
  posi  = 0;
  valu  = 0;
//  point = 0xA002;
  while( posi < Size )
  { //
    if ( !data[ posi ] )
    { // Save the pointer
      posi++;
      valu = data[ posi++ ] & 0xFF;
//      if ( ptr[ valu ] )
//        ShowMessage( "Warning: POINTER[ 0x" + IntToHex( valu, 2 ) + " ] has been assigned a value already!" );
      //
      ptr[ valu ] = 0xA000 + posi;
      // Leave routine
      if ( valu == 0xFF ) break;
    }
    else
    { // Check for other shit
      valu = _MATH[ data[posi] ];
      //
      if ( !valu )
      { // Skip test valu = $00
        while( data[posi] != 0xFF && posi < Size )
          posi++;
        //
        posi++;
        if ( posi >= Size ) break;
      }
      else
      { // Skip Test valu NOT = $00
        posi += valu;
      } // END IF Skip Test
    } // END IF - Main Test

  } // WEND

  //
  ListBox1->Clear( );
  for ( int c = 0; c < 0x100; c++ )
    ListBox1->Items->Add( "POINTER[ 0x" + IntToHex( c, 2 ) + " ] = $" + IntToHex( ptr[ c ], 4 ) );
}// END OF SUB



//-----------------------------//
// Load the program data Bank  //
//-----------------------------//
long __fastcall TForm1::Snes2Pc( long ofs )
{
  LONG val;

  if ( !ofs ) return 0;

  val = (((ofs | 0x800000) - 0x800000) & 0xFF0000) >> 1;
  val+= ((ofs & 0xFFFF) - 0x8000);
  return val;
}// END OF SUB



//-----------------------------//
// Get the length of the file  //
//-----------------------------//
long __fastcall TForm1::FileSize( FILE *tf )
{
  LONG val,
	   	 offset;

  // Test file
  if ( !tf )  return -1;

  // Get size
  offset = ftell( tf );
  fseek( tf, 0, SEEK_END );
  val = ftell( tf );
  fseek( tf, offset, SEEK_SET );

  return val;
}// END OF SUB

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  getcwd( cur_dir, MAXPATH );
}
//---------------------------------------------------------------------------
